package com.yycx.module.bus.provider.handler.cbreturn;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.security.OpenUser;
import com.yycx.module.admin.provider.mapper.BaseUserDeptMapper;
import com.yycx.module.bus.client.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 未退保证金列表
 *
 * @author zyf
 */
@Component("noCbReturnPageListHandler")
public class NoCbReturnPageListHandler implements PageInterceptor<BusOrderDetails> {
    @Autowired
    private BaseUserDeptMapper userDeptMapper;

    @Override
    public void prepare(CriteriaQuery<BusOrderDetails> cq, PageParams pageParams, EntityMap params) {
        cq.clear();
        Integer auth = cq.getInt("auth", 0);
        Long deptId = params.getLong("deptId");
        String beginDate = params.get("beginDate");
        String endDate = params.get("endDate");
        OpenUser openUser = OpenHelper.getUser();
        cq.addSelect(BusOrder.class, "orderId","orderNo","sendDate","supplierId","customBondType");
        cq.addSelect(BusOrderDetails.class,"orderDetailsId","deptId","salePrice","supplierRetDate","supplierRealAmount", "saleNum","saleAmount","customBond","customOverBond","returnCustomBond");
        cq.addSelect(BusSupplier.class,"supplierName");
        cq.addSelect(BusCustom.class,"customName");
        cq.addSelect(BusDrugs.class, "drugsName", "drugsSpec");
        cq.addSelect(BusDept.class, "deptName");
        //未退款
        cq.ne("bod.customBond",CommonConstants.INT_0);
        cq.isNull("bod.tbzjId");
        cq.and(e -> e.eq("supplierRefundState", 2).or().eq("drugs.ownerState", 1));
        cq.eq("bod.orderDetailsState",CommonConstants.INT_1);
        //已回款
        cq.eq("bod.customReturnState",CommonConstants.INT_2);
        cq.like(BusOrder.class, "orderNo");
        if (FlymeUtils.isNotEmpty(beginDate)) {
            cq.ge(BusOrder.class, "sendDate", beginDate);
        }
        if (FlymeUtils.isNotEmpty(endDate)) {
            cq.le(BusOrder.class, "sendDate", endDate);
        }
        cq.eq(true, "bod.companyId", openUser.getCompanyId());
        cq.createJoin(BusDept.class);
        if (auth.equals(CommonConstants.INT_1)) {
            Long userId = openUser.getUserId();
            List<Long> deptIds = userDeptMapper.selectUserDeptIds(userId);
            if (FlymeUtils.isNotEmpty(deptIds)&&FlymeUtils.isEmpty(deptId)) {
                cq.in("bod.deptId", deptIds);
            }else{
                cq.eq("bod.deptId", deptId);
            }
        }
    }

}
